package com.meraklisina.latexeditor;

import java.io.*;

import javax.swing.JTextPane;
import com.meraklisina.latexeditor.mdi.TextEditor.TextEditors;
import java.awt.event.KeyListener;
import java.text.NumberFormat;
import java.util.Vector;

import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.text.MaskFormatter;

public class NavigationComboPreparation extends javax.swing.JPanel  {

	private JTabbedPane tab_Editors;


	
	public NavigationComboPreparation(JTabbedPane editors)
	{
		this.tab_Editors=editors;

		this.setFocusable(true);

	}
	
	
	/** Returns active JTextPane object
	 */
	public JTextPane getTextEditor()
	{
		return new TextEditors().GetJTextPaneAt(this.tab_Editors, this.tab_Editors.getSelectedIndex());
	}
	
	public Object[] ReadText()
	{
		String myText = getTextEditor().getText();
	
	Vector<String> vArr = new Vector<String>();
	int index   = getTextEditor().getText().indexOf("\n")+1;
	int lineCount = 0;
	vArr.add(this.tab_Editors.getTitleAt( this.tab_Editors.getSelectedIndex()));
	while(index > 0)
	{
		String theLine = myText.substring(0, index);
		int itemIndex = theLine.indexOf("\\section");
		int curlyIndex2 = -1;
		int curlyIndex1 = -1;
		lineCount++;
		
		if(itemIndex >= 0 )
		{
			curlyIndex1 =theLine.indexOf("{");
		curlyIndex2 =theLine.indexOf("}");
		if(curlyIndex1 >= 0 && curlyIndex2 >=0  && curlyIndex1 < curlyIndex2 )
		vArr.add(">".concat(theLine.substring(curlyIndex1+1, curlyIndex2).concat(" - ").concat(String.valueOf(lineCount))));
		}
		
		itemIndex = theLine.indexOf("\\subsection");
		
		if(itemIndex >= 0 )
		{
			curlyIndex1 =theLine.indexOf("{");
			curlyIndex2 =theLine.indexOf("}");
			if(curlyIndex1 >= 0 && curlyIndex2 >=0  && curlyIndex1 < curlyIndex2 )
			vArr.add(">>".concat(theLine.substring(curlyIndex1+1, curlyIndex2).concat(" - ").concat(String.valueOf(lineCount))));
			}
		
		itemIndex = theLine.indexOf("\\subsubsection");
		
		if(itemIndex >= 0 )
		{
			curlyIndex1 =theLine.indexOf("{");
			curlyIndex2 =theLine.indexOf("}");
			if(curlyIndex1 >= 0 && curlyIndex2 >=0  && curlyIndex1 < curlyIndex2 )
			vArr.add(">>>".concat(theLine.substring(curlyIndex1+1, curlyIndex2).concat(" - ").concat(String.valueOf(lineCount))));
			}
		
		itemIndex = theLine.indexOf("\\paragraph");
		
		if(itemIndex >= 0 )
		{
			curlyIndex1 =theLine.indexOf("{");
			curlyIndex2 =theLine.indexOf("}");
			if(curlyIndex1 >= 0 && curlyIndex2 >=0  && curlyIndex1 < curlyIndex2 )
			vArr.add(">>>>".concat(theLine.substring(curlyIndex1+1, curlyIndex2).concat(" - ").concat(String.valueOf(lineCount))));
			}

		itemIndex = theLine.indexOf("\\subparagraph");
		
		if(itemIndex >= 0 )
		{
			curlyIndex1 =theLine.indexOf("{");
			curlyIndex2 =theLine.indexOf("}");
			if(curlyIndex1 >= 0 && curlyIndex2 >=0  && curlyIndex1 < curlyIndex2 )
			vArr.add(">>>>>".concat(theLine.substring(curlyIndex1+1, curlyIndex2).concat(" - ").concat(String.valueOf(lineCount))));
			}
		
		myText = myText.substring(index);
		index   = myText.indexOf("\n")+1;
	}
	
	return vArr.toArray();
	}
	public int ConvertToInteger(String theStr)
	{
		return Integer.parseInt(theStr);
	}
	public int Navigate(Object theObj)
	{
		if (theObj.toString().contains("-") == false)
			return -1;
		int ind = theObj.toString().indexOf("-");
		
		String b = theObj.toString().substring(ind+1).trim();

		int lineNo = ConvertToInteger(theObj.toString().substring(ind+1).trim());
	try
		{
		   // lineNo = Integer.getInteger(b);
		}
		catch ( Exception e )
		{
		   
		}

		int index   = getTextEditor().getText().indexOf("\n")+1;
		String txt = getTextEditor().getText();
		int count =1;
		int curIndex =0;
		if(lineNo < 2)
		{
			getTextEditor().setCaretPosition(0);
		}
		else
		{
			while(curIndex != -1 && count < (lineNo -1))
			{
				String cc= getTextEditor().getText().substring(index);
				curIndex = cc.indexOf("\n");
				if(curIndex != -1)
				{
					index += (curIndex +1);
					
				}
				
				
				count++;
			}
			if(curIndex != -1)
			{
				index = index - (count);
				getTextEditor().setCaretPosition(index);
				getTextEditor().setSelectionStart(index);
			}
			else
			{
				getTextEditor().setCaretPosition(getTextEditor().getText().length()-count-1);
				getTextEditor().setSelectionStart(getTextEditor().getText().length()-count-1);	
			}

			
		}
	return index;	
	}
	
	

}
